home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Typography Samples / Tracking Layout ƒ / Tracking Layout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  12.4 KB  |  397 lines  |  [TEXT/KAHL]

  1. /**\
  2. |**| =====================================================================
  3. |**|
  4. |**|    Tracking Layout.c
  5. |**|
  6. |**|    This file contains the calls that this sample needs to make
  7. |**|    the QuickDraw GX shell work correctly.
  8. |**|
  9. |**|    QuickDraw GX Libraries Used:
  10. |**|    "color library.c", "font library.c", "graphics debug library.c",
  11. |**|    "layout library.c", "shape library.c", and "transform library.c".
  12. |**|
  13. |**|    ©1992-1994  Apple Computer, Inc.
  14. |**|    All rights reserved.
  15. |**|
  16. |**| =====================================================================
  17. \**/
  18.  
  19.  
  20. #include "QDGX shell.h"
  21. #include "layout routines.h"
  22. #include "layout library.h"
  23.  
  24.  
  25. /**\
  26. |**| ---------------------------------------------------------------------
  27. |**| PROTOTYPES
  28. |**| ---------------------------------------------------------------------
  29. \**/
  30.  
  31. // funtions required by shell
  32.  
  33. void    DoSetup            (void);
  34. void    DoDraw            (WindowPtr wind, Boolean updating);
  35. OSErr    DoCreateNew        (void);
  36. void    DoDispose        (WindowPtr wind);
  37. void    DoIdle            (WindowPtr wind);
  38. void    DoTeardown        (void);
  39. void    DoClick            (WindowPtr wind, Point p);
  40.  
  41. // private functions
  42.  
  43. OSErr    DoWindowInit        (WindowPtr wind);
  44. void    CreateSampleImage    (WindowPtr wind);
  45.  
  46.  
  47. /**\
  48. |**| ---------------------------------------------------------------------
  49. |**| ENUMS
  50. |**| ---------------------------------------------------------------------
  51. \**/
  52. enum { rWindResource = 128 };
  53.  
  54.  
  55. /**\
  56. |**| ---------------------------------------------------------------------
  57. |**| GLOBALS
  58. |**| ---------------------------------------------------------------------
  59. \**/
  60. // If gDebugging = TRUE, graphics library errors and notices will be posted.  This
  61. // functionality will only work with the "debugging" version of QuickDraw GX.
  62. // If the debugging version is not installed, nothing bad will happen, but these
  63. // functions will not work. 
  64.  
  65. Boolean        gDebugging = true;
  66.  
  67. // Set  "gGiveMeValidation" to TRUE if you want receive run-time validation.
  68.  
  69. Boolean        gGiveMeValidation = true;
  70.  
  71.  
  72. // gGraphicsHeapSize sets the size of the graphics heap created by calling the
  73. // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c.  You can determine
  74. // the amount of graphics heap required by using GraphicsBug.  I'm giving it 600K,
  75. // since we need abunch if we have several windows open.
  76.  
  77. long        gGraphicsHeapSize = 600;
  78.  
  79. // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
  80. // override.  This is so that our override can be native PowerPC code if necessary.
  81.  
  82. GXPrintingEventUPP    gOurPrintingOverrideUPP;
  83.  
  84.  
  85.  
  86. /**\
  87. |**| ---------------------------------------------------------------------
  88. |**| DoSetup()
  89. |**| Here's where we initialize any global variables our application needs.
  90. |**| We have only one at this time -- the universal proc pointer for
  91. |**| our printing override.
  92. |**| ---------------------------------------------------------------------
  93. \**/
  94. void DoSetup (void)
  95. {    // Initialize our printing event override UPP
  96.     gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
  97. }
  98.  
  99.  
  100. /**\
  101. |**| ---------------------------------------------------------------------
  102. |**| DoDraw()
  103. |**| Draw the contents of the window.  The first parameter is the window
  104. |**| to draw, and the second parameter is true if we're updating an existing
  105. |**| image.  If that's the case, we don't want to change anything, but
  106. |**| just draw what's already there.
  107. |**| ---------------------------------------------------------------------
  108. \**/
  109. void DoDraw (WindowPtr wind, Boolean updating)
  110. {
  111.      #pragma unused (updating)
  112.      GXDrawShape (GetDocShape(wind));
  113. }
  114.  
  115.  
  116. /**\
  117. |**| ---------------------------------------------------------------------
  118. |**| DoCreateNew()
  119. |**| This routine is called when a window needs to be created.
  120. |**| ---------------------------------------------------------------------
  121. \**/
  122. OSErr DoCreateNew (void)
  123. {
  124.     OSErr        err = noErr;
  125.     WindowPtr    wind;
  126.     
  127. // Get and create our window from the resource fork
  128.  
  129.     wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
  130.  
  131. // Attach a default gxViewPort to it, create and iInitialize our
  132. // private data for it, and add a sample image to its page shape.
  133.  
  134.     if ( wind == NULL )
  135.         return (MemError());
  136.  
  137.     GXIgnoreGraphicsNotice(transform_already_set);
  138.     SetDefaultViewPort(GXNewWindowViewPort(wind));            
  139.     GXPopGraphicsNotice();
  140.     
  141.     err = DoWindowInit(wind);
  142.     if ( err != noErr )
  143.         return err;
  144.     
  145.     CreateSampleImage(wind);
  146.     return err;
  147. }
  148.  
  149.  
  150. /**\
  151. |**| ---------------------------------------------------------------------
  152. |**| DoDispose()
  153. |**| This routine is called when a window needs to be disposed of.
  154. |**| ---------------------------------------------------------------------
  155. \**/
  156. void DoDispose (WindowPtr wind)
  157. {
  158.     TH_Doc    doc;
  159.     
  160. // You should always dispose of your GX graphics objects before tossing your window.
  161. // Why?  It's generally good form and this approach guarantees that everything is
  162. // disposed.  If you had not disposed of everything, the call to DisposeWindow should
  163. // dispose of the objects. If you are running the debugging version of QuickDraw GX
  164. // with notices set, you will receive a notice that you had not disposed of everything.
  165. // You can turn notices on in this file by setting gDebugging = TRUE (above).
  166.     
  167.     if ( wind != NULL )
  168.     {
  169.         doc = (TH_Doc)GetWRefCon(wind);        // Remember, this is where we stored our private data.
  170.         GXDisposeShape(GetDocShape(wind));     // Dispose of this doc's shape.
  171.         GXDisposeJob(GetDocJob(wind));        // Dispose of this doc's print job.
  172.         DisposHandle((Handle) doc);            // Dispose of our private data.
  173.         DisposeWindow(wind);                // Dispose of the window.
  174.     }
  175. }
  176.  
  177.  
  178. /**\
  179. |**| ---------------------------------------------------------------------
  180. |**| DoIdle()
  181. |**| This routine is called to do things while idling through the event loop.
  182. |**| ---------------------------------------------------------------------
  183. \**/
  184. void DoIdle (WindowPtr wind)
  185. {
  186. }
  187.  
  188.  
  189. /**\
  190. |**| ---------------------------------------------------------------------
  191. |**| DoTeardown()
  192. |**| This routine is called just before we quit to remove anything 
  193. |**| persistent that might have been setup by DoSetup().
  194. |**| ---------------------------------------------------------------------
  195. \**/
  196. void DoTeardown (void)
  197. {
  198.     DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
  199. }
  200.  
  201.  
  202. /**\
  203. |**| ---------------------------------------------------------------------
  204. |**| DoClick()
  205. |**| ---------------------------------------------------------------------
  206. \**/
  207. void DoClick(WindowPtr window, Point p)
  208. {
  209. }
  210.  
  211.  
  212.  
  213.  
  214.  
  215. /**\
  216. |**| ---------------------------------------------------------------------
  217. |**| DoWindowInit()
  218. |**| In this function we create and initialize the the private document
  219. |**| structure for a new window.  This structure contains the print job and
  220. |**| the shape which is drawn in the window.  We store this data in a handle
  221. |**| and hang it off the window's refCon field for easy retrieval.  By doing
  222. |**| this, rather than using globals, we can create many windows containing
  223. |**| unique print jobs and shapes.
  224. |**| ---------------------------------------------------------------------
  225. \**/
  226. OSErr DoWindowInit (WindowPtr wind)
  227. {
  228.     OSErr    err = noErr;
  229.     gxJob    docJob;
  230.     gxShape    docPage;
  231.     TH_Doc    windDoc;
  232.  
  233.  
  234. // Create the page shape. We set the unique items attribute to make sure that each item
  235. // added to the picture has a unique reference. If this attribute was not set, we would
  236. // not see all copies of anything we add to the shape multiple times -- we'd just see
  237. // the last version added.        
  238.  
  239.     docPage = GXNewShape(gxPictureType);
  240.     GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
  241.     
  242.     
  243. // Create a print job for this document.  This will be the same as the system default until
  244. // the user goes through the dialogs for Page Setup or Print…
  245.  
  246.     err = GXNewJob(&docJob);
  247.     
  248.     
  249. // If there are no errors, create a handle the size of our document structure and store
  250. // the print job and page shape in it.  Store the handle in the window's refCon field so
  251. // that we can get at it.  (Note that the utility routines "GetDocJob" and "GetDocShape"
  252. // can be used to do this easily.
  253.  
  254.     if ( err == noErr )
  255.     {
  256.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  257.  
  258.         if ( windDoc == NULL )
  259.             err = MemError();
  260.         else
  261.         {
  262.             (*windDoc)->docJob = docJob;
  263.             (*windDoc)->docPage = docPage;
  264.             SetWRefCon(wind, (long) windDoc);
  265.         }
  266.  
  267. // Now install our application override for PrintingEvent so that we can
  268. // support the new movable-modal printing dialog boxes.
  269.  
  270.         GXInstallApplicationOverride(docJob, gxPrintingEvent, gOurPrintingOverrideUPP);
  271.  
  272.     }
  273.  
  274.     return err;
  275. }
  276.  
  277.  
  278. /**\
  279. |**| ---------------------------------------------------------------------
  280. |**| CreateSampleImage()
  281. |**| This function creates primitive shapes and adds them to the window's page shape.
  282. |**| ---------------------------------------------------------------------
  283. \**/
  284. void CreateSampleImage (WindowPtr wind)
  285. {
  286.     Rect    ourWindowRect = wind->portRect; // the rectangle for this window
  287.                                             // in QuickDraw coordinates
  288.     gxShape layoutNormal;                // the regular layout shape we build
  289.     gxShape layoutTight;                // a layout with tighter tracking
  290.     gxShape layoutLoose;                // a layout with looser tracking
  291.     gxShape thePage;                    // this window's document shape
  292.     gxRunControls runControls;            // run controls for the layout shape
  293.     gxLayoutOptions layoutOptions;        // options for the layout shape
  294.     
  295. // These are the five text runs for this layout shape, in pieces because they're
  296. // in different languages
  297.     
  298.     char *text = "track track track track track";
  299.     
  300.     gxPoint posn;                        // the position of the layout shape
  301.  
  302.  
  303. // make default gxLayoutOptions and gxRunControls structures
  304.  
  305.     InitializeLayoutOptions (&layoutOptions);
  306.     InitializeRunControls (&runControls);
  307.     
  308.     
  309. // Position the layout half way down the left edge of the window. Set 
  310. // the layout's width to the window's width and set the flushness to 1/2, this
  311. // will cause the layout to center in the window.  Note that ourWindowRect is
  312. // not in fixed coordinates, so we have to make it fixed to use it.
  313.  
  314.     layoutOptions.width = ff(ourWindowRect.right - ourWindowRect.left);
  315.     layoutOptions.flush = fract1/2;
  316.  
  317.     posn.x = 0;
  318.     posn.y = ff((ourWindowRect.bottom - ourWindowRect.top) / 2);
  319.     
  320.  
  321. // Create the layouts we'll use.  NewSingleLayout is a library routine found
  322. // in layout library.c to create a layout based on one style.
  323.  
  324.     layoutNormal = NewSingleLayout(
  325.                 text,                             // the text for the layout
  326.                 (char *) "\pHoefler Text",         // the font name to use
  327.                 ff(32),                         // the point size to use
  328.                 &layoutOptions,                 // our default layout options
  329.                 &posn,                             // the shape's position
  330.                 0,                                 // attributes (none)
  331.                 &runControls,                    // our run controls
  332.                 nil,                             // the run features array (none)
  333.                 0,                                 // count of zero features = 0
  334.                 nil);                            // no style run overrides
  335.                 
  336.     posn.y -= ff(40);                            // move up forty points
  337.     
  338. // Now adjust the tracking to be very tight per the font designer's choices
  339.  
  340.     runControls.track = ff(-2);
  341.     
  342.     layoutTight = NewSingleLayout(
  343.                 text,                             // the text for the layout
  344.                 (char *) "\pTimes Roman",         // the font name to use
  345.                 ff(32),                         // the point size to use
  346.                 &layoutOptions,                 // our default layout options
  347.                 &posn,                             // the shape's position
  348.                 0,                                 // attributes (none)
  349.                 &runControls,                    // our run controls
  350.                 nil,                             // the run features array (tight)
  351.                 0,                                 // count of zero features = 0
  352.                 nil);                            // no style run overrides
  353.                 
  354.     posn.y += ff(80);                            // now draw forty points below center
  355.     
  356.     
  357. // Now adjust the tracking to be very loose per the font designer's choices
  358.  
  359.     runControls.track = ff(2);
  360.     
  361.     layoutLoose = NewSingleLayout(
  362.                 text,                             // the text for the layout
  363.                 (char *) "\pTimes Roman",         // the font name to use
  364.                 ff(32),                         // the point size to use
  365.                 &layoutOptions,                 // our default layout options
  366.                 &posn,                             // the shape's position
  367.                 0,                                 // attributes (none)
  368.                 &runControls,                    // our run controls
  369.                 nil,                             // the run features array (loose)
  370.                 0,                                 // count of zero features = 0
  371.                 nil);                            // no style run overrides
  372.                 
  373.     
  374. // Retrieve the page shape so we can add to it.
  375.  
  376.     thePage = GetDocShape(wind);
  377.     
  378. // Add the layout to the window's picture shape.  AddToShape is in shape library.c.
  379.  
  380.     AddToShape(thePage, layoutNormal);
  381.     AddToShape(thePage, layoutTight);
  382.     AddToShape(thePage, layoutLoose);
  383.  
  384. // Now that all shapes are added to the picture, dispose of them
  385.  
  386.     GXDisposeShape(layoutNormal);
  387.     GXDisposeShape(layoutTight);
  388.     GXDisposeShape(layoutLoose);
  389.     
  390.  
  391. // Invalidate the window's portRect so that everything gets updated.
  392.     
  393.     SetPort(wind);
  394.     InvalRect(&ourWindowRect);
  395. }
  396.  
  397.